package com.arkisv.common.model;/**
 * Created by fb on 2017/2/7.
 */

import org.apache.commons.beanutils.BeanUtils;

/**
 * 全局返回
 * Created by fb on 2017/2/7.
 */
public class Res {
    private int code;
    private String msg;
    private Object data;

    public Res() {}
    public Res(int code,String msg,Object data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }
    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return convertList2Page(data);
    }

    public void setData(Object data) {
        this.data = data;
    }

    public static Res success() {
        return new Res(0,null,null);
    }

    public static Res success(Object data) {
        return new Res(0,null,data);
    }
    public static Res res(int code,String msg) {
        return new Res(code,msg,null);
    }
    public static Res res(int code,String msg,Object data) {
        return new Res(code,msg,data);
    }

    /**
     * 将结果集(list)封装成page对象
     * @param list
     * @return
     */
    @SuppressWarnings("rawtypes")
    private static Object convertList2Page(Object list) {
        if (list instanceof com.github.pagehelper.Page) {
            Page page = new Page();
            com.github.pagehelper.Page pageHelper = (com.github.pagehelper.Page) list;
            try {
                BeanUtils.copyProperties(page, pageHelper);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return page;
        }
        return list;
    }
}
